﻿@using SimpleIdServer.CredentialIssuer.Domains;
@using SimpleIdServer.CredentialIssuer.Website.Stores.CredentialIssuer;
@using System.Globalization;
@inherits Fluxor.Blazor.Web.Components.FluxorComponent
@inject IState<CredentialsState> credentialsState
@inject IDispatcher dispatcher
@inject NotificationService notificationService
@inject DialogService dialogService

@if (!credentialsState.Value.IsLoading)
{
    <RadzenButton class="mb-1" Click="@(args => Issue())" Icon="add" Text="@Global.IssueCredential" ButtonStyle="ButtonStyle.Primary" Size="ButtonSize.Medium"></RadzenButton>
    <RadzenRow Gap="1rem">
        @foreach (var credential in credentialsState.Value.Credentials)
        {
            <RadzenColumn Size="4">
                <CredentialViewer ServerId="@CredentialConfiguration.ServerId" Display="@GetDisplay(CredentialConfiguration)" Credential="@credential">
                    <ActionsContent>
                        <RadzenButton Click=@((evt) => Remove(credential)) Variant="Variant.Text">@Global.Delete</RadzenButton>
                    </ActionsContent>
                </CredentialViewer>
            </RadzenColumn>
        }
    </RadzenRow>
}
else
{
    <RadzenStack Orientation="Orientation.Horizontal" AlignItems="AlignItems.Center" JustifyContent="JustifyContent.Center" Wrap="FlexWrap.Wrap" Class="rz-m-12" Gap="2rem">
        <RadzenProgressBarCircular ShowValue="true" Mode="ProgressBarMode.Indeterminate" Size="ProgressBarCircularSize.Large">
            <Template>@Global.Loading</Template>
        </RadzenProgressBarCircular>
    </RadzenStack>
}

@code {
    [Parameter]
    public Domains.CredentialConfiguration CredentialConfiguration { get; set; }

    protected override void OnParametersSet()
    {
        base.OnParametersSet();
        if (CredentialConfiguration == null) return;
        var act = new GetCredentialInstancesAction { CredentialConfigurationId = CredentialConfiguration.Id };
        dispatcher.Dispatch(act);
        SubscribeToAction<IssueCredentialInstanceSuccessAction>((act) =>
        {
            notificationService.Notify(NotificationSeverity.Info, Global.CredentialIssued);
            StateHasChanged();
        });
        SubscribeToAction<RemoveCredentialInstanceSuccessAction>((act) =>
        {
            notificationService.Notify(NotificationSeverity.Info, Global.CredentialRemoved);
            StateHasChanged();
        });
        SubscribeToAction<IssueCredentialInstanceFailureAction>((act) =>
        {
            notificationService.Notify(NotificationSeverity.Error, act.ErrorMessage);
            StateHasChanged();
        });
    }

    async void Issue()
    {
        var result = (await dialogService.OpenAsync<IssueCredentialModal>(Global.IssueCredential)) as Domains.Credential;
        if (result == null) return;
        var act = new IssueCredentialInstanceAction { CredentialConfigurationId = CredentialConfiguration.Id, CredentialId = result.CredentialId, ExpirationDateTime = result.ExpirationDateTime, IssueDateTime = result.IssueDateTime.Value, Subject = result.Subject };
        dispatcher.Dispatch(act);
    }

    void Remove(Credential credential)
    {
        var act = new RemoveCredentialInstanceAction { Id = credential.Id };
        dispatcher.Dispatch(act);
    }

    CredentialConfigurationTranslation GetDisplay(Domains.CredentialConfiguration configuration)
    {
        return configuration.Displays.SingleOrDefault(c => c.Locale == CultureInfo.CurrentUICulture.ToString());
    }
}